package org.dinner.order.dao.impl;

import java.util.Date;
import java.util.List;

import org.dinner.order.dao.OrderDAO;
import org.dinner.order.dao.OrderItemDAO;
import org.dinner.order.model.Order;
import org.dinner.order.model.OrderItem;
import org.dinner.order.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class OrderDAOImpl extends BaseDAOImpl<Order> implements OrderDAO {
	private static final String ENTITY_NAME = "Order";
	
	@Autowired
	private OrderItemDAO orderItemDAO;
	
	public Order getByShippmentDate(int suId, Date date) {
		List list = sessionFactory.getCurrentSession().getNamedQuery(ENTITY_NAME + ".getByShippmentDate")
				.setDate("date", DateUtils.truncateTime(date))
				.setInteger("suId", suId).list();
		
		if (list != null) {
			return (Order) list.get(0);
		} else {
			return null;
		}
		
	}

	public Order getByMenuId(int suId, int menuId) {
		List list = sessionFactory.getCurrentSession().getNamedQuery(ENTITY_NAME + ".getByMenuId")
				.setInteger("menuId", menuId)
				.setInteger("suId", suId).list();
		
		if (list != null) {
			return (Order) list.get(0);
		} else {
			return null;
		}
	}

	public void addOrderItem(int orderId, int itemId) {
		Order order = this.getById(orderId);
		OrderItem orderItem = orderItemDAO.getById(itemId);
		order.getItems().add(orderItem);
	}

	@Override
	protected String getQueryPrefix() {
		return ENTITY_NAME;
	}

}
